package com.example.cqcet_demo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.cqcet_demo.R;
import com.example.cqcet_demo.adapter.MainFragmentAdapter;
import com.example.cqcet_demo.fragment.HomeFragment;
import com.example.cqcet_demo.fragment.RecordFragment;
import com.example.cqcet_demo.fragment.UserInfoFragment;
import com.example.cqcet_demo.uitls.CloseActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private ViewPager vp_main;
    private RadioGroup rg_main;
    private long firstTime = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CloseActivity.add(MainActivity.this);

        initView();
        List<Fragment> fragments=new ArrayList<Fragment>();

        fragments.add(new RecordFragment());
        fragments.add(new HomeFragment());
        fragments.add(new UserInfoFragment());


        MainFragmentAdapter mfadapter = new MainFragmentAdapter(getSupportFragmentManager(),fragments);
        vp_main.setAdapter(mfadapter);
        vp_main.setCurrentItem(1);
        //vp_main.setOffscreenPageLimit(3);
        vp_main.addOnPageChangeListener(new MyOnPageChangeListener());
        rg_main.setOnCheckedChangeListener(new MyOnCheckedChangeListener());

    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
                case R.id.rb_record:
                    vp_main.setCurrentItem(0);
                    break;
                case R.id.rb_home:
                    vp_main.setCurrentItem(1);
                    break;
                case R.id.rb_user:
                    vp_main.setCurrentItem(2);
                    break;
            }
        }

    }

    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                //当前为页卡1
                case 0:
                    rg_main.check(R.id.rb_record);
                    break;
                    //当前为页卡2
                case 1:
                    rg_main.check(R.id.rb_home);
                    break;
                //当前为页卡3
                case 2:
                    rg_main.check(R.id.rb_user);
                    break;
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

    }

    private void initView() {
        vp_main = (ViewPager)findViewById(R.id.vp_main);
        rg_main = (RadioGroup) findViewById(R.id.rg_1);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            long time = System.currentTimeMillis();
            if(time-firstTime>2000){
                Toast.makeText(MainActivity.this,"再按一次返回键,退出程序",Toast.LENGTH_SHORT).show();
                firstTime = time;
            }else{
                CloseActivity.close();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }


}
